package io.renren.modules.seller.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.seller.dao.BrandDao;
import io.renren.modules.seller.entity.BrandEntity;
import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.service.BrandService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private BrandDao brandDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<BrandEntity> findAll() {
        return this.list();
    }

    @Override
    public PageResult findPage(Map<String, Object> params) {
        String name = (String) params.get("name");
        String firstChar = (String) params.get("firstChar");
        IPage<BrandEntity> page = this.page(
                new Query<BrandEntity>().getPage(params),
                new QueryWrapper<BrandEntity>().
                        eq(StringUtils.isNotBlank(name), "name", name).
                        eq(StringUtils.isNotBlank(firstChar), "first_char", firstChar)
        );
        return new PageResult(page.getTotal(), page.getRecords());
    }

    @Override
    public List<Map> selectOptionList() {
        return brandDao.selectOptionList();
    }

}
